<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Prunus Core Model
 *
 * @author		agosto
 * @date		  20090504
 * @copyright	Copyright (c) 2009, 7dedos
 * @link		http://www.7dedos.com.ar
 */
 
class PrunusModel extends Model {
	protected $ci;

	public function __construct()	{
		parent::__construct();
		$this->ci =& get_instance();
	}

/* USUARIOS ****************************************************************/
  public function getUser($usuId) {
    $this->ci->db->select('usu_name,usu_idioma,usu_firstName,usu_lastName,usu_men,men_mei');
  	$this->ci->db->from('pr_usuarios');
  	$this->ci->db->join('pr_menus','usu_men=men_id','left');
		$this->ci->db->limit(1);
		$this->ci->db->where('usu_id', $usuId);
		$this->ci->db->where('usu_alive', 1);
		return $this->ci->db->get();
	}
  public function getDeletedOrNotUser($usuId) {
    $this->ci->db->select('usu_name,usu_idioma,usu_firstName,usu_lastName');
  	$this->ci->db->from('pr_usuarios');
		$this->ci->db->limit(1);
		$this->ci->db->where('usu_id', $usuId);
		return $this->ci->db->get();
	}
  
  public function insertUser($name,$idioma,$firstName,$lastName,$pwdHash) {
    if (is_null($firstName)) $firstName = '';
    if (is_null($lastName)) $lastName = '';
    $data = array('usu_name' => $name,    'usu_pwdHash' => $pwdHash,
                 'usu_idioma' => $idioma, 'usu_firstName' => $firstName,
                 'usu_lastName' => $lastName, 'usu_alive' => 1);
    $this->ci->db->insert('pr_usuarios', $data); 
    return ($this->ci->db->insert_id());
  }
  
  public function deleteUser($usuId) {
    $data = array('usu_alive' => 0);
		$this->ci->db->where('usu_id', $usuId);
    $this->ci->db->update('pr_usuarios', $data); 
  }
  public function killUser($usuId) {
    $this->emptyUserPermisos($usuId);
    $this->emptyUserRoles($usuId);
    $sql = "delete from pr_usuarios where usu_id=" . $usuId;
    return $this->ci->db->query($sql);
  }
  
  public function getUsuIdFromName($userName) {
    $query = $this->ci->db->select('usu_id')->where('usu_name', $userName)->where('usu_alive', 1)->limit(1)->get('pr_usuarios');
    if ($query->num_rows() == 1) { return ($query->row()->usu_id); } else { return 0; }
  }
  
  public function getUsuPwdHah($usuId) {
    $query = $this->ci->db->select('usu_pwdHash')->where('usu_id', $usuId)->where('usu_alive', 1)->limit(1)->get('pr_usuarios');
    if ($query->num_rows() == 1) { return ($query->row()->usu_pwdHash); } else { return ''; }
  }
  
  public function setUsuPwdHash($usuId, $pwdHash) {
    $data = array('usu_pwdHash' => $pwdHash);
		$this->ci->db->where('usu_id', $usuId);
    $this->ci->db->update('pr_usuarios', $data); 
  } 
  
  public function setUsuIdioma($usuId, $idioma) {
    $data = array('usu_idioma' => $idioma);
		$this->ci->db->where('usu_id', $usuId);
    $this->ci->db->update('pr_usuarios', $data); 
  }
  
  public function setUsuFirstName($usuId, $firstName) {
    $data = array('usu_firstName' => $firstName);
		$this->ci->db->where('usu_id', $usuId);
    $this->ci->db->update('pr_usuarios', $data); 
  }
  
  public function setUsuLastName($usuId, $lastName) {
    $data = array('usu_lastName' => $lastName);
		$this->ci->db->where('usu_id', $usuId);
    $this->ci->db->update('pr_usuarios', $data); 
  }
  public function insertUserRole($usuId,$rolId) {
    $sql = "replace into pr_usuarios_roles (pur_usu,pur_rol)";
    $sql .= " select " . $usuId . "," . $rolId;
    return $this->ci->db->query($sql);
  }
  public function getUsuRoles($usuId=0, $exclusionArray='') {
    if ($usuId === 0) $usuId = $this->ci->prunuslib->usuId;
    $sql =  "SELECT rol_id,rol_name,rol_builtin";
    $sql .= " FROM pr_usuarios_roles inner join pr_roles on pur_rol=rol_id and rol_alive=1";
    $sql .= " where rol_alive=1";
    $sql .= " and pur_usu=" . $usuId;
    
    if ($exclusionArray !== '') {
      $exclusionList = implode(",", $exclusionArray);
      if (strlen($exclusionList) > 0) $sql .= " and rol_id not in (" . $exclusionList . ")";
    }
    $sql .= " order by rol_id";
    return $this->ci->db->query($sql);    
  }
  
  public function emptyUserRoles($usuId) {
    $sql = "delete from pr_usuarios_roles where pur_usu=" . $usuId;
    return $this->ci->db->query($sql);
  }
  public function getUsuPermisosAccion($usuId, $accId) {
    //devuelvo la lista de permisos para la accion por cada rol y para el usuario
    $sql  = "select per_rol rol,0 usu,per_permiso permiso";
    $sql .= " from pr_usuarios_roles";
    $sql .= " inner join pr_permisos on per_rol=pur_rol";
    $sql .= " where pur_usu=" . $this->ci->prunuslib->usuId;
    $sql .= " and per_acc=" . $accId;
    $sql .= " union";
    $sql .= " select 0 rol, per_usu, per_permiso permiso";
    $sql .= " from pr_permisos";
    $sql .= " where per_usu=" . $usuId;
    $sql .= " and per_acc=" . $accId;
    return $this->ci->db->query($sql);
  }
  public function emptyUserPermisos($usuId) {
    $sql = "delete from pr_permisos where per_usu=" . $usuId;
    return $this->ci->db->query($sql);
  }
/***************************************************************************/








/* ROLES *******************************************************************/
  public function getRoles($limit = 0, $offset = 0, $exclusionArray='') {
    $sql =  "SELECT rol_id,rol_name,rol_builtin";
    $sql .= " FROM pr_roles";
    $sql .= " where rol_alive=1";
    
    if ($exclusionArray !== '') {
      $exclusionList = implode(",", $exclusionArray);
      if (strlen($exclusionList) > 0) $sql .= " and rol_id not in (" . $exclusionList . ")";
    }
    
    $sql .= " order by rol_id";
    if ($limit != 0) $sql .= " limit $offset,$limit" ;
    return $this->ci->db->query($sql);    
  }
  function getRole($rolId) {
    $sql =  "SELECT rol_id,rol_name,rol_builtin";
    $sql .= " FROM pr_roles";
    $sql .= " where rol_alive=1";
    $sql .= " and rol_id=" . $rolId;
    return $this->ci->db->query($sql);    
  }
  public function getDeletedOrNotRole($usuId) {
    $sql =  "SELECT rol_id,rol_name,rol_builtin";
    $sql .= " FROM pr_roles";
    $sql .= " where rol_id=" . $rolId;
    return $this->ci->db->query($sql);    
  }
  public function insertRole($name) {
    $data = array('rol_name' => $name,
                  'rol_builtin' => 0,
                  'rol_alive' => 1);
    $this->ci->db->insert('pr_roles', $data); 
    return ($this->ci->db->insert_id());
  }
  public function deleteRole($rolId) {
    $data = array('rol_alive' => 0);
		$this->ci->db->where('rol_id', $rolId);
    $this->ci->db->update('pr_roles', $data); 
  }
  public function killRole($rolId) {
    $this->emptyRolePermisos($rolId);
    $this->emptyRolUsers($rolId);
    $sql = "delete from pr_roles where rol_id=" . $rolId;
    return $this->ci->db->query($sql);
  }
  public function emptyRolUsers($rolId) {
    $sql = "delete from pr_usuarios_roles where pur_rol=" . $rolId;
    return $this->ci->db->query($sql);
  }
  public function emptyRolePermisos($rolId) {
    $sql = "delete from pr_permisos where per_rol=" . $rolId;
    return $this->ci->db->query($sql);
  }
/***************************************************************************/




  
  


/* EVENTOS *****************************************************************/
  public function insertEvent($usu,$eveId,$p1,$p2,$p3,$p4,$p5) {
    $this->db->set('evl_usu', $usu);
    $this->db->set('evl_stamp', 'CURRENT_TIMESTAMP()', FALSE);
    $this->db->set('evl_eve', $eveId);
    $this->db->set('evl_param1', $p1);
    $this->db->set('evl_param2', $p2);
    $this->db->set('evl_param3', $p3);
    $this->db->set('evl_param4', $p4);
    $this->db->set('evl_param5', $p5);
    $this->db->insert('pr_eventlog'); 
  }
  public function getEventGroup($eveId) {
    $sql = "select eve_gre";
    $sql .=" from pr_eventos";
    $sql .=" where eve_id=" . $eveId;
    $query = $this->ci->db->query($sql);
    if ($query->num_rows() == 1) { return ($query->row()->eve_gre); } else { return (0); }
  }
  public function getEventsFromUser($usuId, $from = null, $to = null, $eveList = null, $greList = null) {
    $sql = "select evl_stamp,evl_usu,evl_eve,evl_param1,evl_param2,evl_param3,evl_param4,evl_param5,";
    $sql .=" usu_name,";
    $sql .=" gre_id";
    $sql .=" from pr_eventlog";
    $sql .=" inner join pr_usuarios on evl_usu=usu_id";
    $sql .=" inner join pr_eventos on evl_eve=eve_id";
    $sql .=" inner join pr_grupos_eventos on eve_gre=gre_id";
    $sql .=" where usu_id=" . $usuId;
    if (! is_null($from))    $sql .=" and evl_stamp>='" . $from . "'";
    if (! is_null($to))      $sql .=" and evl_stamp<='" . $to . "'";
    if (! is_null($eveList)) $sql .=" and evl_eve in (" . $eveList . ")";
    if (! is_null($greList)) $sql .=" and eve_gre in (" . $greList . ")";
    return $this->ci->db->query($sql);
  }
/***************************************************************************/


/* MENUS *******************************************************************/
  public function getMenuRootItem($menId) {
    $sql =  "select men_mei,mei_uri";
    $sql .= " from pr_menus inner join pr_menus_items on men_mei=mei_id";
    $sql .= " where men_id=" . $menId;
    return $this->ci->db->query($sql);    
  }
  public function getMenuItemChildren($meiId) {
    $sql =  "select mei_id,mei_uri";
    $sql .= " from pr_menus_items";
    $sql .= " where mei_mei=" . $meiId;
    return $this->ci->db->query($sql);
  }
  
  //ahorrando queries
  public function getMenuItems() {
    $sql = "select mei_id,mei_mei,mei_uri from pr_menus_items order by mei_id";
    return $this->ci->db->query($sql);
    
  }

/***************************************************************************/

}
